//
//  MBProgressHUD+ErrorMessage.m
//  QA
//
//  Created by 李小龙 on 2017/5/2.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "MBProgressHUD+ErrorMessage.h"
//#import "LoginViewController.h"

#if __has_feature(objc_arc)
#define MB_AUTORELEASE(exp) exp
#define MB_RELEASE(exp) exp
#define MB_RETAIN(exp) exp
#else
#define MB_AUTORELEASE(exp) [exp autorelease]
#define MB_RELEASE(exp) [exp release]
#define MB_RETAIN(exp) [exp retain]
#endif

@implementation MBProgressHUD (ErrorMessage)

//原来的方法名 里面 是新的方法
+ (MB_INSTANCETYPE)myShowHUDAddedTo:(UIView *)view labelText:(NSString *)labelText animated:(BOOL)animated {
    view = [UIApplication sharedApplication].keyWindow;
    
    MBProgressHUD *hud = [[self alloc] initWithView:view];
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeCustomView;
    hud.detailsLabel.text = labelText;
    hud.margin = 10.f;
    //指定距离中心点的X轴和Y轴的偏移量，如果不指定则在屏幕中间显示
    //        hud.yOffset = self.view.center.y - 50;
    //        hud.xOffset = 0.0f;
    
    [view addSubview:hud];
    [hud showAnimated:animated];
    [hud hideAnimated:YES afterDelay:1.5];
    
    return MB_AUTORELEASE(hud);
    
}

/**
 * @brief 显示网络非200的错误情况，有默认文案。
 */
+ (MB_INSTANCETYPE)showError:(UIView *)view title:(NSString *)text code:(NSString *)code animated:(BOOL)animated {
    view = [UIApplication sharedApplication].keyWindow;
    
//    if ([code isEqualToString:@"204"]) {
//        [[UserInfo shareInstance] logout];
//        
//        UIViewController *topVC = ((UINavigationController *)((BaseTabbarViewController *)[UIApplication sharedApplication].keyWindow.rootViewController).selectedViewController).topViewController;
//        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录，登录后才可继续操作" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//            
//            LoginViewController *loginVC = [[LoginViewController alloc] init];
//            BaseNavigationViewController *nav = [[BaseNavigationViewController alloc] initWithRootViewController:loginVC];
////            [topVC.navigationController pushViewController:loginVC animated:YES];
//            [topVC presentViewController:nav animated:YES completion:nil];
//            
//        }];
//        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        [alertC addAction:action];
//        [alertC addAction:actionCancel];
//        [topVC presentViewController:alertC animated:YES completion:nil];
//        return nil;
//    }
    
    [MBProgressHUD hideHUDForView:view animated:YES];
    MBProgressHUD *hud = [[self alloc] initWithView:view];
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeCustomView;
    hud.margin = 10.f;
    NSString *str = [NSString getCodeMessage:code];
    if (str.length > 0) {
        hud.detailsLabel.text = str;
    } else {
        if (text.length > 0) {
            hud.detailsLabel.text = text;
        } else {
            hud.detailsLabel.text = NetWorkFailMessage;
        }
    }
    
    
    [view addSubview:hud];
    [hud showAnimated:animated];
    [hud hideAnimated:YES afterDelay:1.5];
    
    return MB_AUTORELEASE(hud);
}

+ (MB_INSTANCETYPE)showHUDProgress:(UIView *)view animated:(BOOL)animated {
    if (view == nil) {
        return nil;
    }
    MBProgressHUD *hud = [[self alloc] initWithView:view];
    hud.removeFromSuperViewOnHide = YES;
    [view addSubview:hud];
    [hud showAnimated:animated];
    
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [UIColor clearColor];
    
    for (UIActivityIndicatorView *view in hud.bezelView.subviews) {
        if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
            [view setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
            break;
        }
    }
    
    return hud;
}

@end
